import $ from './library/jquery.js';
// import { cookie } from './library/cookie.js';

let id = location.search.split('=')[1];

$.ajax({
    type: "get",
    url: "../../interface/getItem.php",
    data: {
        id:id
    },
    dataType: "json",
    success: function (res) {
        
        console.log(res)
        let picture = JSON.parse(res.picture);

        console.log(picture)

        let temp = `<div class="mi-detail">
                <div class="img-left">
                    <div class="img-box">
                        <img src="../${picture[1].src}" class="box-img " data-original="" alt="" style="width: 560px;height: 560px;">
                    </div>
                </div>
            </div>
            <div class="product-con fl">
                <div>
                    <h2>${res.title}</h2>
                    <div class="pro">${res.det}</div>
                    <p class="company-info">小米自营</p>
                    <span class="price-info">${res.price} 元</span>
                </div>
                <div class="line"></div>
                <div class="address-box">
                    <div class="product-address">
                        <div class="address-con">
                            <div class="address-info">
                                <span>北京</span>
                                <span>北京市</span>
                                <span>海淀区</span>
                                <span>清河街道</span>
                                <a href="" class="edit">修改</a>
                            </div>
                            <div class="info">
                                <div class="desc">
                                    <span>有现货</span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="buy-box-child">
                    <div class="title">
                        选择版本
                    </div>
                    <ul class="clearfix">
                        <li>6GB+128GB</li>
                        <li>
                            8GB+128GB
                        </li>
                        <li>
                            8GB+256GB
                        </li>
                        <div style="clear:both"></div>
                    </ul>
                </div>
                <div>
                <input type="number" value="1" min="1" max="${res.num}"
                id="num" style="display:none">
                </div>
                <div class="btn-box">
                    <div class="sale-btn fl">
                        <input type="button" value="加入购物车" id="addItem">
                    </div>
                    <div class="favorite-btn fl">
                        喜欢
                    </div>
                </div>
            </div>`;


        $('.swiper').append(temp).find('#addItem').on('click', function() {
            addItem(res.id, res.price, $('#num').val());
        });

    }

});



// function addItem(id, price, num) {
//     let shop = cookie.get('shop'); // 获得cookie数据
//     let product = {
//         id,
//         price,
//         num
//     };

//     if (shop) { // 判断购物车是否有添加过数据
//         shop = JSON.parse(shop); //将JSON字符串转回数组



//         // 判断购物车中是否存在该商品
//         if (shop.some(elm => elm.id == id)) {
//             // 修改数量
//             shop.forEach(el => {
//                 el.id == id ? el.num = num : null;
//             });
//         } else {
//             shop.push(product);
//         }

//     } else {
//         shop = []; // 初始没有数据 初始化一个空数组
//         shop.push(product); // 将第一个商品添加进数组
//     }


//     cookie.set('shop', JSON.stringify(shop), 1);
// }